home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 12.5 KB | 487 lines |
- package symantec.itools.awt;
-
-
- import java.awt.Graphics;
- import java.awt.Color;
- import java.awt.Event;
-
-
- /**
- * A HorizontalSlider component. This component is used to select one value
- * from a continuous range of values. It has a movable thumb in front of a
- * gauge with ticks marks on it.
- * <p>
- * @see symantec.itools.awt.Slider
- * @see symantec.itools.awt.VerticalSlider
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- // 02/15/97 RKM Added validate - Fixes problem where calls to setMinValue & setMaxValue
- // would not work if called after reshape
- // 02/27/97 RKM Merged in Scott's change to use the background color of the component
-
- public class HorizontalSlider
- extends Slider
- {
- /**
- * Length of the gauge ticks in pixels.
- */
- protected static final int TICK_HEIGHT = 4;
- private static final int BORDER_X = 15;
- private static final int BORDER_Y = 10;
-
- private HorizontalSliderTick tick[];
- private HorizontalSliderThumb thumb;
-
- /**
- * Constructs a default HorizontalSlider. The ticks
- * are drawn on both sides of the gauge with a frequency of 1.
- * The minimum value is 1. The maximum value is 10. The
- * border is shown.
- */
- public HorizontalSlider()
- {
- this.thumb = new HorizontalSliderThumbBoth();
- this.style = TICK_BOTH;
- this.min = 1;
- this.max = 10;
- this.freq = 1;
-
- prevPos =
- curPos = 0;
-
- width = 200;
- height = 50;
-
- showBorder = true;
-
- tick = null;
- }
-
- /**
- * Sets the current slider tick mark style.
- * @see #getTickStyle
- * @see Slider#TICK_TOP
- * @see Slider#TICK_BOTTOM
- * @see Slider#TICK_BOTH
- */
- public void setTickStyle(int style)
- {
- if (this.style != style)
- {
- this.style = style;
-
- switch (style)
- {
- case TICK_TOP :
- thumb = new HorizontalSliderThumbTop();
- break;
-
- case TICK_BOTTOM :
- thumb = new HorizontalSliderThumbBot();
- break;
-
- default :
- thumb = new HorizontalSliderThumbBoth();
- break;
-
- }
-
- invalidate();
- }
- }
-
- /**
- * Returns the current slider tick mark style.
- * @see #setTickStyle
- * @see Slider#TICK_TOP
- * @see Slider#TICK_BOTTOM
- * @see Slider#TICK_BOTH
- */
- public int getTickStyle()
- {
- return style;
- }
-
- /**
- * Processes MOUSE_DOWN events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_DOWN
- * event. These events occur when the mouse button is pressed while
- * inside this component.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see java.awt.Component#mouseUp
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseDown(Event e, int x, int y)
- {
- moveThumb(x, true);
-
- return true;
- }
-
- /**
- * Processes MOUSE_DRAG events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_DRAG
- * event. These events occur when the mouse is moved around inside this
- * component while the button is pressed.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see java.awt.Component#mouseMove
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseDrag(Event e, int x, int y)
- {
- moveThumb(x, false);
-
- return true;
- }
-
- private void do_reshape(int w, int h)
- {
- int hb = BORDER_X;
- int vb = BORDER_Y;
-
- if (w < hb)
- hb = w / 4;
-
- if (h < vb)
- vb = h / 4;
-
- int x0 = hb;
- int x1 = w - hb;
- int y0 = vb;
- int y1 = h - vb;
-
- if (x0 == 0)
- x0 = 1;
-
- if (x1 == 0)
- x1 = 1;
-
- if (y0 == 0)
- y0 = 1;
-
- if (y1 == 0)
- y1 = 1;
-
- int n = (max - min) / freq + 1;
-
- tick = new HorizontalSliderTick[n];
-
- int hs = (x1 - x0) / (n - 1), ch;
-
- for (int i = 0; i < n; ++i)
- {
- ch = i * hs;
- tick[i] = new HorizontalSliderTick(x0 + ch, y0, y1, ch);
- }
-
- thumb.resize(hs / 2, y1 - y0 - TICK_HEIGHT - 1);
- }
-
- /**
- * Moves and/or resizes this component.
- * This is a standard Java AWT method which gets called to move and/or
- * resize this component. Components that are in containers with layout
- * managers should not call this method, but rely on the layout manager
- * instead.
- *
- * @param x horizontal position in the parent's coordinate space
- * @param y vertical position in the parent's coordinate space
- * @param width the new width
- * @param height the new height
- */
- public void reshape(int x, int y, int w, int h)
- {
- width = w;
- height = h;
-
- do_reshape(w, h);
-
- super.reshape(x, y, w, h);
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- if (tick.length == 0)
- return;
-
- HorizontalSliderTick t;
-
- g.clipRect(0, 0, width, height);
-
- thumb.draw(g, tick[curPos]);
-
- if (prevPos != curPos)
- thumb.clip(g, tick[prevPos]);
-
- g.setColor(getBackground());
- g.fillRect(0, 0, width, height);
-
- g.setColor(Color.black);
-
- int x0, x1, y, w = width - 1, h = height - 1;
- boolean end;
-
- if (showBorder)
- g.drawRect(0, 0, w, h);
-
- for (int i = 0; i < tick.length; ++i)
- {
- end = i == 0 || i == tick.length - 1;
-
- t = tick[i];
-
- if (style == TICK_TOP || style == TICK_BOTH)
- g.drawLine(t.x, t.y0 + (end ? 0 : 1), t.x, t.y0 + TICK_HEIGHT);
-
- if (style == TICK_BOTTOM || style == TICK_BOTH)
- g.drawLine(t.x, t.y1 - TICK_HEIGHT, t.x, t.y1 - (end ? 0 : 1));
- }
-
- t = tick[0];
-
- y = (t.y1 + t.y0) / 2;
- x0 = t.x - 5;
- x1 = tick[tick.length - 1].x + 5;
-
- g.drawLine(x0, y, x1, y);
-
- g.setColor(Color.gray);
- g.drawLine(x1 + 1, y - 1, x0 - 1, y - 1);
- g.drawLine(x0 - 1, y - 1, x0 - 1, y + 1);
-
- g.setColor(Color.lightGray);
- g.drawLine(x0, y + 1, x1 + 1, y + 1);
- g.drawLine(x1 + 1, y + 1, x1 + 1, y);
-
- g.setColor(Color.white);
- g.drawLine(x0 - 1, y + 2, x1 + 2, y + 2);
- g.drawLine(x1 + 2, y + 2, x1 + 2, y - 1);
-
- g.clipRect(0, 0, width, height);
-
- thumb.draw(g, tick[curPos]);
-
- prevPos = curPos;
- }
-
- /**
- * This routine updates the thumb position, paints the HorizontalSlider, and
- * posts a new action event, as needed. If the thumb position has
- * changed or the forcePost parameter is true the component will be painted
- * and an action event posted.
- * @param pos the new thumb position
- * @param forcePost true forces a repaint and posting of an action message
- * even if the thumb position hasn't changed
- */
- protected void doMove(int pos, boolean forcePost)
- {
- if (tick == null)
- {
- prevPos = curPos = pos;
- return;
- }
-
- if (pos >= tick.length)
- pos = tick.length - 1;
-
- if (pos != curPos || forcePost)
- {
- prevPos = curPos;
- curPos = pos;
- paint(getGraphics());
-
- postEvent(new Event(this, Event.ACTION_EVENT, new Integer(curPos * freq + min)));
- }
- }
-
- private void moveThumb(int x, boolean forcePost)
- {
- if(tick.length > 1)
- {
- int dist = tick[1].x - tick[0].x;
-
- if (dist == 0)
- return;
-
- int newPos = (x - tick[0].x) / dist;
-
- if (newPos < 0)
- newPos = 0;
-
- if (((x - tick[0].x) % dist) > (dist / 2))
- ++newPos;
-
- doMove(newPos, forcePost);
- }
- }
-
- /**
- * Ensures that this component is laid out properly, as needed.
- * This is a standard Java AWT method which gets called by the AWT to
- * make sure this component and its subcomponents have a valid layout.
- * If this component was made invalid with a call to invalidate(), then
- * it is laid out again.
- *
- * @see java.awt.Component#invalidate
- */
- public void validate() {
- super.validate();
-
- do_reshape(width, height);
- }
- }
-
-
- class HorizontalSliderTick
- {
- int x;
- int y0;
- int y1;
- int v;
-
- HorizontalSliderTick(int ix, int iy0, int iy1, int iv)
- {
- x = ix;
- y0 = iy0;
- y1 = iy1;
- v = iv;
- }
- }
-
-
- abstract class HorizontalSliderThumb
- {
- protected int x;
- protected int y;
- protected int width;
- protected int height;
- protected Graphics g;
- protected HorizontalSliderTick t;
-
- void resize(int width, int height)
- {
- x = (this.width = width) / 2;
- y = (this.height = height) - HorizontalSlider.TICK_HEIGHT - 1 - 1;
- }
-
- abstract void draw(Graphics g, HorizontalSliderTick t);
-
- protected void draw(int x0, int y0, int x1, int y1)
- {
- g.drawLine(t.x + x0, t.y0 + y0 + HorizontalSlider.TICK_HEIGHT + 1,
- t.x + x1, t.y0 + y1 + HorizontalSlider.TICK_HEIGHT + 1);
- }
-
- protected void initDraw(Graphics g, HorizontalSliderTick t)
- {
- this.g = g;
- this.t = t;
-
- g.setColor(Color.lightGray);
- g.fillRect(t.x - x + 1, t.y0 + 2 + HorizontalSlider.TICK_HEIGHT + 1, width - 2, y - 2);
-
- g.setColor(Color.white);
- }
-
- void clip(Graphics g, HorizontalSliderTick t)
- {
- g.clipRect(t.x - width / 2, t.y0, width + 1, height + 1);
- }
- }
-
-
- class HorizontalSliderThumbBoth extends HorizontalSliderThumb
- {
- void draw(Graphics g, HorizontalSliderTick t)
- {
- super.initDraw(g, t);
-
- draw(-x, y, -x, 1);
- draw(-x, 1, x, 1);
-
- g.setColor(Color.black);
- draw(-x, y, x, y);
- draw(x, y, x, 1);
-
- g.setColor(Color.gray);
- draw(1 - x, y - 1, x - 1, y - 1);
- draw(x - 1, y - 1, x - 1, 2);
- }
- }
-
-
- class HorizontalSliderThumbTop extends HorizontalSliderThumb
- {
- void draw(Graphics g, HorizontalSliderTick t)
- {
- super.initDraw(g, t);
-
- int a = y / 5;
-
- draw(-x, y, -x, a);
- draw(-x, a, 0, 1);
-
- g.setColor(Color.black);
- draw(0, 1, x, a);
- draw(x, a, x, y);
- draw(x, y, -x, y);
-
- g.setColor(Color.gray);
- draw(0, 2, x - 1, a);
- draw(x - 1, a, x - 1, y - 1);
- draw(x - 1, y - 1, 1 - x, y - 1);
- }
- }
-
-
- class HorizontalSliderThumbBot extends HorizontalSliderThumb
- {
- void draw(Graphics g, HorizontalSliderTick t)
- {
- super.initDraw(g, t);
-
- int a = height - HorizontalSlider.TICK_HEIGHT - 1 - 1 - y / 5;
-
- draw(x, 1, -x, 1);
- draw(-x, 1, -x, a);
- draw(-x, a, 0, y - 1);
-
- g.setColor(Color.black);
- draw(0, y, x, a);
- draw(x, a, x, 1);
-
- g.setColor(Color.gray);
- draw(0, y - 1, x - 1, a);
- draw(x - 1, a, x - 1 , 2);
- }
- }
-
-